﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;

namespace BOH.DataManagement
{
    public partial class ManageAccessRight : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                RightBind();
        }

        private void RightBind()
        {
            DataSet dst = null;
            dst = UserClass.dstGetRight();

            BindRightList(dst);
        }

        private void BindRightList(DataSet dst)
        {
            AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
            if (AspNetPager1.RecordCount != 0)
            {
                labNoRecord.Visible = false;
                AspNetPager1.Visible = true;
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dst.Tables[0].DefaultView;
                pds.AllowPaging = true;
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                pds.PageSize = AspNetPager1.PageSize;
                RightList.DataSource = pds;
                RightList.DataBind();
            }
            else
            {
                RightList.DataSource = null;
                RightList.DataBind();
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            RightBind();
        }

        public string getAccess(string Levels)
        {
            string returnvalue = string.Empty;
            switch (Levels.Trim().ToUpper())
            {
                case "A":
                    returnvalue = "系统管理员";
                    break;
                case "U":
                    returnvalue = "单位用户组";
                    break;
                case "C":
                    returnvalue = "中心用户组";
                    break;
                case "G":
                    returnvalue = "主管局用户组";
                    break;
                case "S":
                    returnvalue = "学科组管理员";
                    break;
                case "P":
                    returnvalue = "个人用户组";
                    break;
                case "Q":
                    returnvalue = "试题组管理员";
                    break;
                default:
                    break;
            }

            return returnvalue;
        }

        protected void RightList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            RightList.EditIndex = e.NewEditIndex;
            RightBind();
        }

        protected void RightList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            RightList.EditIndex = -1;
            RightBind();
        }

        protected void RightList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Normal && e.Row.RowState != DataControlRowState.Alternate))
            {
                HiddenField hid = (HiddenField)(e.Row.FindControl("hiddenRight"));
                DropDownList drpAccess = (DropDownList)(e.Row.FindControl("drpAccessList"));
                drpAccess.SelectedValue = hid.Value;
            }
        }

        protected void RightList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string rightid = ((HiddenField)RightList.Rows[e.RowIndex].FindControl("hiddenrightID")).Value;
            string RightType = ((DropDownList)RightList.Rows[e.RowIndex].FindControl("drpAccessList")).SelectedValue;
            string RightCode = ((TextBox)RightList.Rows[e.RowIndex].FindControl("txtRightCode")).Text.Trim();
            string RightName = ((TextBox)RightList.Rows[e.RowIndex].FindControl("txtRightName")).Text.Trim();

            if (rightid.Trim().Length > 0)
                UserClass.updateRight(rightid,RightName, RightType,RightCode);
            //else
            //{
            //    UserClass.InsertRight(UserID, RightType, "A");
            //}
            RightList.EditIndex = -1;
            RightBind();
        }

        protected void butSaveRight_Click(object sender, EventArgs e)
        {
            string rightType = drpRightList.SelectedValue.Trim();
            string rightCode = txtRightCode.Text.Trim();
            string rightName = txtRightName.Text.Trim();

            UserClass.InsertRight(rightName, rightCode, rightType);

            RightBind();

            txtRightCode.Text = "";
            txtRightName.Text = "";

        }
    }
}
